package bookshop.controller.backend;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import bookshop.domain.Category;
import bookshop.exception.ApplicationException;
import bookshop.exception.DataAccessException;
import bookshop.service.CategoryService;
import bookshop.service.helper.CategoryQueryHelper;
import bookshop.service.impl.CategoryServiceImpl;
import bookshop.utils.Page;

public class CategoryServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		String act = request.getParameter("act");

		if ("input_ctg".equals(act)) {
			request.getRequestDispatcher(
					"jsps/backend/category/input_category.jsp").forward(
					request, response);
		} else if ("create".equals(act)) {

			String ctgName = request.getParameter("ctgname");

			Category ctg = new Category();
			ctg.setCtgCreateTime(new java.util.Date());
			ctg.setCtgName(ctgName);
			CategoryService ctgService = new CategoryServiceImpl();
			String errMsg = null;
			try {
				ctgService.createCategory(ctg);
			} catch (ApplicationException e) {
				e.printStackTrace();
				errMsg = e.getMessage();
			}
			if(errMsg==null){
			response.sendRedirect("categoryMgr?act=qryloadall");
			}else{
				request.setAttribute("errMsg", errMsg);
				request.getRequestDispatcher("jsps/backend/category/input_category.jsp").forward(request, response);
			}
		} else if ("qryloadall".equals(act)) {
			//设置一个查询参数存储容器
			CategoryQueryHelper helper=new CategoryQueryHelper();
			
			//根据情况，把提交过来的查询参数写入helper,以方便传给后台，进行记录的条件过滤。
			if(request.getParameter("qryCtgId")!=null && request.getParameter("qryCtgId").length()!=0)
				helper.setQryCtgId(Integer.parseInt(request.getParameter("qryCtgId")));
			
			if(request.getParameter("qryCtgName")!=null && request.getParameter("qryCtgName").length()!=0)
				helper.setQryCtgName(request.getParameter("qryCtgName"));
			
			//创建页面对象,如果外界没有指定要第几页,默认就是第一页,如果有指定,比如 bookMgr?act=loadBooks&pageNo=3,则取第3页。
			Page page=new Page();
			if(request.getParameter("pageNo")!=null)
				page.setPageNo(Integer.parseInt(request.getParameter("pageNo")));
			
			CategoryService ctgService = new CategoryServiceImpl();
			//helper未指定，也就是说，所有的属性为null, 则分页就是对所有记录的分页。
			request.setAttribute("page", ctgService.loadCategoryByCondition(helper, page));
			request.getRequestDispatcher("jsps/backend/category/list_category.jsp").forward(request,response);
		}else if("preModify".equals(act)){
			int ctgId = Integer.parseInt(request.getParameter("ctgid"));
			CategoryService ctgService = new CategoryServiceImpl();
			request.setAttribute("ctg", ctgService.getCategoryById(ctgId));
			request.getRequestDispatcher("jsps/backend/category/update_category.jsp").forward(request, response);
		}else if("update".equals(act)){
			int ctgId = Integer.parseInt(request.getParameter("ctgid"));
			String ctgName = request.getParameter("ctgname");
			CategoryService ctgService = new CategoryServiceImpl();
			Category ctg = ctgService.getCategoryById(ctgId);
			ctg.setCtgName(ctgName);
			ctg.setCtgCreateTime(new java.util.Date());
			ctgService.updateCategory(ctg);
			
			response.sendRedirect("categoryMgr?act=qryloadall");
		}else if("remove".equals(act)){
			int ctgId = Integer.parseInt(request.getParameter("ctgid"));
			CategoryService ctgService = new CategoryServiceImpl();
			try {
				ctgService.removeCtg(ctgId);
			} catch (ApplicationException e) {
				e.printStackTrace();
				request.getSession().setAttribute("errMsg", e.getMessage());
			}
			response.sendRedirect("categoryMgr?act=qryloadall");
			
		}

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		this.doGet(request, response);
	}

}
